/*
 * @lc app=leetcode.cn id=3 lang=swift
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
class Solution {
    func lengthOfLongestSubstring(_ s: String) -> Int {
        var sArr = Array(s)
        var window: [Character: Int] = [:]
        var left = 0, right = 0
        var res = 0
        while right < sArr.count {
            let rChar = sArr[right]
            right += 1
            window[rChar, default: 0] += 1

            while window[rChar]! > 1 {
                let lChar = sArr[left]
                left += 1
                window[lChar]! -= 1
            }
            res = max(res, right - left)
        }
        return res
    }
}
// @lc code=end

